#!/usr/bin/perl
#tgtman-lib.pl
do '../web-lib.pl';
&init_config();
%access = &get_module_acl();

sub get_gstr()
{
	open(FG, "/etc/ietd.conf");
	my @tg=<FG>;
	close(FG);
	my $gstr = $tg[0];
	$gstr =~ s/\r|\n//g;
	$gstr =~ s/#|\s//g;
	return $gstr;
}
sub get_target_list()
{
	$index=-1; $i=0;
	open(FH, "/etc/ietd.conf");
	
        while($line=<FH>) {
		
		$line =~ s/\r|\n//g;
		if($line =~ /^Target\s(\S+):(\S+)/) {
			$index++;
			$gstr=$1; $name=$2;
			$tid=$index;
			$iet_cache[$index]->{'tid'} = $tid;
			$iet_cache[$index]->{'gstr'} = $gstr;
			$iet_cache[$index]->{'name'} = $name;
			$iet_cache[$index]->{'fullname'} = $gstr.":".$name;
		}
		elsif($line =~ /^IncomingUser/){
		   if($line =~ /^IncomingUser\s(\S+)\s(\S+)/) {
			$user=$1;
			$pwd=$2;
		   }else{
			$user = '';
			$pwd = '';	
		   }
		   if($index > -1){			
			$iet_cache[$index]->{'user'} = $user;
			$iet_cache[$index]->{'pwd'} = $pwd;
			#push(@iet, $user);
		   }
		}
		elsif($line =~ /^Lun\s(\d+)\sPath=(\S+),\S+/){
			$lunid=$1;
			$device=$2;
			if($index > -1){
				$iet_cache[$index]->{'lunid'} = $lunid;
				$iet_cache[$index]->{'device'} = $device;
				#push(@iet, "aa");
			}
		}
		elsif($line =~ /^(Alias\s\S+)/){
			$alias = $1;
			if($index > -1){
				$iet_cache[$index]->{'alias'} = $alias;
			}
		}
		else{
			next;
		}
		#$i++;
	}	
	close(FH);
return @iet_cache;
}

sub get_session()
{
   $fullname = $_[0];
   open(FS, "/proc/net/iet/session");
   @sess=<FS>;
   close(FS);
   $num=@sess;
   for($i=0; $i<$num; $i++) {
	$sess[$i] =~ s/\r|\n//g;
	if($sess[$i] =~ /^tid:\d+\s+name:($fullname)/) {
		$j = $i + 1;
		$k = $i + 2;
		if($sess[$j] =~ /sid/) {
		   return 'connected';
		}
		else{
		   return 'unconnected';
		}
	}else{
		next;
	}
   }
   return 'service stop';
}

sub get_tid()
{

}
sub crt_target()
{
	my $tname = $_[0];
	my $dev = $_[1];
	my $user = $_[2];
	my $pwd = $_[3];
	my $devmark = $_[5];
	my $gstr = &get_gstr();
	open(FN, ">>/sysvol/etc/ietd.conf");
	print FN "Target ".$gstr.":".$tname."\n";
	if($user){
		print FN "IncomingUser ".$user." ".$pwd."\n";
	}else{
		print FN "IncomingUser\n";
	}
	print FN "Lun 0 Path=".$dev.",Type=blockio\n";
	print FN "Alias ".$devmark."\n";
	print FN "ImmediateData		Yes\n";
	print FN "MaxOutstandingR2T	16\n";
	close(FN);
	return 1;
}
sub del_target()
{
	my $trgt = $_[0];
	my @tglist = &get_target_list(); 
	open(FT, "/etc/ietd.conf");
	@oldconf = <FT>;
	close(FT);
	open(FTN, ">/etc/ietd.conf.b");
	print FTN $oldconf[0];
	foreach $t (@tglist) {
		if($t->{'fullname'} ne $trgt->{'fullname'}) {
			print FTN "Target ".$t->{'fullname'}."\n";
			if($t->{'user'}){
				print FTN "IncomingUser ".$t->{'user'}." ".$t->{'pwd'}."\n";
			}else{
				print FTN "IncomingUser\n";
			}
			print FTN "Lun 0 Path=".$t->{'device'}.",Type=blockio\n";
			print FTN $t->{'alias'}."\n";
			
			print FTN "ImmediateData	Yes\n";
			print FTN "MaxOutstandingR2T	16\n";
		}
	}
	close(FTN);
	`mv -f /etc/ietd.conf.b /sysvol/etc/ietd.conf`;
}
sub modify_target()
{
	my $tg = $_[0];
	my $tuser = $_[1];
	my $tpwd = $_[2];
	my @trgt = &get_target_list();
	if(!&checkuser($tuser)){
		return "user error";
	}
	if($tuser) {
		if(&checkpwd($tpwd)) {
		
		}else{
			return "pwd error";
		}
	}
        open(FT, "/etc/ietd.conf");
        @oldconf = <FT>;
        close(FT);
        open(FTN, ">/sysvol/etc/ietd.conf");
        print FTN $oldconf[0];
	foreach $ti (@trgt) {
		if($ti->{'fullname'} ne $tg->{'fullname'}) {
                        print FTN "Target ".$ti->{'fullname'}."\n";
                        if($ti->{'user'}){
                                print FTN "IncomingUser ".$ti->{'user'}." ".$ti->{'pwd'}."\n";
                        }else{
                                print FTN "IncomingUser\n";
                        }
			print FTN "Lun 0 Path=".$ti->{'device'}.",Type=blockio\n";
                        print FTN $ti->{'alias'}."\n";
			print FTN "ImmediateData	Yes\n";
			print FTN "MaxOutstandingR2T	16\n";
		}else{
			print FTN "Target ".$ti->{'fullname'}."\n";	
			print FTN "Lun 0 Path=".$ti->{'device'}.",Type=blockio\n
";
			print FTN "ImmediateData	Yes\n";
			print FTN "MaxOutstandingR2T	16\n";
			if($tuser){
				print FTN "IncomingUser ".$tuser." ".$tpwd."\n";
			}else{
				print FTN "IncomingUser\n";
			}
			print FTN $ti->{'alias'}."\n";	
		}
	}	
	close(FTN);
	#`cp -f /etc/ietc.conf.b /sysvol/etc/ietd.conf`;
}
sub checkname()
{
	my $name=$_[0];
	if(!$name){
		return 0;
	}
	if(($name =~ /[^a-z]/)){
		return 0;
	}
	my $len = length($name);
	if(($len < 2) || ($len > 8)){
		return 0;
	}
	return 1;
}
sub checkdev()
{
	my $dev = $_[0];
	if($dev eq ''){
		return 0;
	}
	if(!(-b $dev)){
	 	return 0;
	}
	return 1;
}
sub checkuser()
{
	my $user = $_[0];
	if($user =~ /[^a-z]/){
		return 0;
	}
	my $ulen = length($user);
	if(($ulen < 2) || ($ulen > 8)){
		return 0;
	}
	return 1;
}
sub checkpwd()
{
	my $pwd=$_[0];
	my $len=length($pwd);
	if($len < 12){
		return 0;
	}
	if($pwd =~ /[^a-zA-Z0-9]/){
		return 0;
	}
	return 1;
}

sub vol_mount_status
{
        my (@p, @mstatus);

        @mstatus = (0, undef);
        open(MTAB, "/etc/mtab");
        while(<MTAB>) {
                chop;
                s/#.*$//g;
                if (!/\S/) { next; }
                @p = split(/\s+/, $_);
                if ($p[0] eq $_[0]) {
                        #found
                        $mstatus[0] = 1;
                        $mstatus[1] = $p[1];
                        return @mstatus;
                }
        }
        close(MTAB);
        return @mstatus;
}

sub list_iprule{
	
		$target = $_[0];
		my ($rulelineallow, $rulelinedeny, @allowip, @denyip);
    open(IA, "/etc/initiators.allow");
    my @allowfile=<IA>;
    close(IA);

    open(MT, "/etc/initiators.deny");
    my @denyfile=<MT>;
    close(MT);
    
		foreach (@allowfile) {
			$_ =~ s/\r|\n//g;
			if ($_ =~ /^$target\s+(\S+)$/) {
				$rulelineallow = $1;				
			}
		}

		if($rulelineallow) {
			@allowip = split(/,/, $rulelineallow)
		}

		foreach (@denyfile) {
			$_ =~ s/\r|\n//g;
			if ($_ =~ /^$target\s+(\S+)$/) {
				$rulelinedeny = $1;
			}
		}
		
		if($rulelinedeny) {
			@denyip = split(/,/, $rulelinedeny)
		}
				
    my $mlines=@allow;
    print "<table width=800 class=mode2><tr>\n";
    print "<td  valign=top><div class=main><table  class=mode1 border=0 bordercolor=#33ccff cellspacing=1 cellpadding=1 width=100%>\n";
    print "<tr class=title1> <th align=left width=400>$text{'ipaddr'}</th> ",
          "<th align=left width=200>$text{'rule'}</th>";
          #"<th align=left>$text{'modcomment'}</th> </tr>\n";
    $i = 0;
    foreach(@allowip){
    	$i++;
			print "<tr class=mode1><td><input type=radio name=ruleline value=\"$i\">$_</td><td>Allow</td></tr>";
		}
		foreach(@denyip){
			$i++;
			print "<tr class=mode1><td><input type=radio name=ruleline value=\"$i\">$_</td><td>Deny</td></tr>";
		}
    print "</table></div></td>\n";

    print "<td valign=top width=160><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
    printf "<tr><td align=center><input type=submit name=mcreate value=\"$text{'ruleadd'}\" onclick=\'return on_add_click();\' class=button></td></tr>\n";
    print "<tr><td align=center><input type=submit name=mdelete value=\"$text{'ruledelete'}\" onclick=\'return on_ruledel_click($i, \"$text{'confirm_delrule'}\", \"$text{'chooseone'}\");\' class=button></td></tr>\n";
		#print "<tr><td align=center><input type=submit name=mdelete value=\"$text{'ruledelall'}\" onclick=\'return on_mdeleteall_click($i, \"$text{'confirm_delallrule'}\");\' class=button></td></tr>\n";

    print "</table></td></tr>";
    #print "<tr><td colspan=2><i>$text{'hint'}</i></td></tr>";
    print "</table>\n";    
}
1;
